package com.ruoyi.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.manage.domain.EventSurgery;
import com.ruoyi.manage.mapper.EventSurgeryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.manage.mapper.SurgeryPlanMapper;
import com.ruoyi.manage.domain.SurgeryPlan;
import com.ruoyi.manage.service.ISurgeryPlanService;
import org.springframework.transaction.annotation.Transactional;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 手术计划Service业务层处理
 *
 * @author liuyangyang
 * @date 2025-05-28
 */
@Service
public class SurgeryPlanServiceImpl implements ISurgeryPlanService {
    @Autowired
    private SurgeryPlanMapper surgeryPlanMapper;
    @Autowired
    private EventSurgeryMapper eventSurgeryMapper;
    @Autowired
    private EventSurgeryServiceImpl eventSurgeryServiceImpl;

    /**
     * 查询手术计划
     *
     * @param surgeryId 手术计划主键
     * @return 手术计划
     */
    @Override
    public SurgeryPlan selectSurgeryPlanBySurgeryId(Long surgeryId) {
        return surgeryPlanMapper.selectSurgeryPlanBySurgeryId(surgeryId);
    }

    /**
     * 查询手术计划列表
     *
     * @param surgeryPlan 手术计划
     * @return 手术计划
     */
    @Override
    public List<SurgeryPlan> selectSurgeryPlanList(SurgeryPlan surgeryPlan) {
        return surgeryPlanMapper.selectSurgeryPlanList(surgeryPlan);
    }

    /**
     * 新增手术计划
     *
     * @param surgeryPlan 手术计划
     * @return 结果
     */
    @Override
    public int insertSurgeryPlan(SurgeryPlan surgeryPlan) {
        surgeryPlan.setCreateTime(DateUtils.getNowDate());
        return surgeryPlanMapper.insertSurgeryPlan(surgeryPlan);
    }

    /**
     * 修改手术计划
     *
     * @param surgeryPlan 手术计划
     * @return 结果
     */
    @Override
    public int updateSurgeryPlan(SurgeryPlan surgeryPlan) {
        surgeryPlan.setUpdateTime(DateUtils.getNowDate());
        return surgeryPlanMapper.updateSurgeryPlan(surgeryPlan);
    }

    /**
     * 批量删除手术计划
     *
     * @param surgeryIds 需要删除的手术计划主键
     * @return 结果
     */
    @Override
    public int deleteSurgeryPlanBySurgeryIds(Long[] surgeryIds) {
        return surgeryPlanMapper.deleteSurgeryPlanBySurgeryIds(surgeryIds);
    }

    /**
     * 删除手术计划信息
     *
     * @param surgeryId 手术计划主键
     * @return 结果
     */
    @Override
    public int deleteSurgeryPlanBySurgeryId(Long surgeryId) {
        return surgeryPlanMapper.deleteSurgeryPlanBySurgeryId(surgeryId);
    }


    /**
     * 开始手术计划
     *
     * @param surgeryId 手术计划主键
     * @return 结果
     */
    @Override
    public int startSurgery(Long surgeryId) {
        return surgeryPlanMapper.startSurgery(surgeryId);
    }

    @Override
    public int cancelSurgery(Long surgeryId, String status) {
        SurgeryPlan surgeryPlan = new SurgeryPlan();
        surgeryPlan.setSurgeryId(surgeryId);
        surgeryPlan.setStatus(status);
        surgeryPlan.setCancelBy(getUsername());
        surgeryPlan.setCancelTime(DateUtils.getNowDate());
        return surgeryPlanMapper.updateSurgeryPlan(surgeryPlan);
    }

    @Transactional
    @Override
    public int outSurgery(EventSurgery eventSurgery) {
        SurgeryPlan surgeryPlan = new SurgeryPlan();
        surgeryPlan.setSurgeryId(eventSurgery.getSurgeryId());
        surgeryPlan.setStatus("1");
        surgeryPlanMapper.updateSurgeryPlan(surgeryPlan);
        List<EventSurgery> eventSurgeryList = new ArrayList<>();
        eventSurgeryList.add(eventSurgery);
        eventSurgeryServiceImpl.insertEventSurgery(eventSurgeryList);
        return 1;
    }

    @Override
    @Transactional
    public int resetSurgery(Long surgeryId, String status) {
        List<EventSurgery> eventSurgeryList = eventSurgeryMapper.selectEventSurgeryBySurgeryId(surgeryId);
        Long[] eventSurgeryIds = new Long[eventSurgeryList.size()];
        for (int i = 0; i < eventSurgeryList.size(); i++) {
            eventSurgeryIds[i] = eventSurgeryList.get(i).getEventSurgeryId();
        }
        eventSurgeryMapper.deleteEventSurgeryDetailByEventSurgeryIds(eventSurgeryIds);
        return eventSurgeryMapper.deleteEventSurgeryByEventSurgeryIds(eventSurgeryIds);
    }


}
